package net.xxcl.kygl.service;

import java.util.List;

import net.xxcl.kygl.mapper.ProjectMapper;
import net.xxcl.kygl.mapper.SbpsyjMapper;
import net.xxcl.kygl.pojo.Criteria;
import net.xxcl.kygl.pojo.Project;
import net.xxcl.kygl.pojo.Sbpsyj;
import net.xxcl.kygl.pojo.SbpsyjKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional()
public class SbpsyjService {
    @Autowired
    private SbpsyjMapper sbpsyjMapper;
    @Autowired
    private ProjectMapper projectMapper;

    private static final Logger logger = LoggerFactory.getLogger(SbpsyjService.class);

    public int countByExample(Criteria example) {
        int count = this.sbpsyjMapper.countByExample(example);
        logger.debug("count: {}", count);
        return count;
    }

    public Sbpsyj selectByPrimaryKey(SbpsyjKey key) {
        return this.sbpsyjMapper.selectByPrimaryKey(key);
    }

    public List<Sbpsyj> selectByExample(Criteria example) {
        return this.sbpsyjMapper.selectByExample(example);
    }

    public int deleteByPrimaryKey(SbpsyjKey key) {
        return this.sbpsyjMapper.deleteByPrimaryKey(key);
    }

    public int updateByPrimaryKeySelective(Sbpsyj record) {
        return this.sbpsyjMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(Sbpsyj record) {
        return this.sbpsyjMapper.updateByPrimaryKey(record);
    }

    public int deleteByExample(Criteria example) {
        return this.sbpsyjMapper.deleteByExample(example);
    }

    public int updateByExampleSelective(Sbpsyj record, Criteria example) {
        return this.sbpsyjMapper.updateByExampleSelective(record, example);
    }

    public int updateByExample(Sbpsyj record, Criteria example) {
        return this.sbpsyjMapper.updateByExample(record, example);
    }

    public int insert(Sbpsyj record) {
        return this.sbpsyjMapper.insert(record);
    }

    public int insertSelective(Sbpsyj record) {
        return this.sbpsyjMapper.insertSelective(record);
    }

    /**
     * 指派专家
     * @param expertids
     * @param projectid
     * @return
     */
    public int appointExperts(String expertids,Integer projectid){
        Criteria criteria = new Criteria();
        criteria.put("projectid",projectid);
        //先把之前的删除
        sbpsyjMapper.deleteByExample(criteria);
        Sbpsyj sbpsyj = new Sbpsyj();
        sbpsyj.setProjectid(projectid);
        String[] str = expertids.split(",");
        for(int i=0;i<str.length;i++){
            //第一个为专家组组长
            if(i==0){
                sbpsyj.setIsleader(true);
            } else {
                sbpsyj.setIsleader(false);
            }
            sbpsyj.setUserid(Integer.parseInt(str[i]));
            sbpsyjMapper.insertSelective(sbpsyj);
        }
        Project project = new Project();
        project.setId(projectid);
        //指派专家成功，状态改变
        project.setState(1);
        projectMapper.updateByPrimaryKeySelective(project);
        return str.length;
    }

    public int countBySbpsyj(Criteria example) {
        int count = this.projectMapper.countBySbpsyj(example);
        logger.debug("count: {}", count);
        return count;
    }

    public List<Project> selectBySbpsyj(Criteria example) {
        return this.projectMapper.selectBySbpsyj(example);
    }
}